from django import forms
from app01.utils import encrypt
from app01.forms.Bootstrap import BootstrapForm
from django.forms import Form, fields, widgets, ModelForm
from app01 import models


class ClassForm(BootstrapForm, ModelForm):
    title = fields.RegexField(
        '计本\d+',
        label='班级名称',

    )

    class Meta:
        model = models.Class
        fields = "__all__"
    #     labels = {
    #         "title": "班级"
    #     }


class StudentForm(BootstrapForm, Form):
    name = fields.CharField(label='姓名', max_length=32)
    mail = fields.EmailField(label='邮箱', max_length=32)
    age = fields.IntegerField(
        label='年龄',
        min_value=16,
        max_value=32,
        error_messages={
            'min_value': "年龄区间为16~32，请输入正确的值",
            'max_value': "年龄区间为16~32，请输入正确的值"
        },
    )
    cls_id = fields.IntegerField(
        label='所属班级',
        # widget=widgets.Select(choices=[(1,'上海'),(2,'北京')])
        widget=widgets.Select(
            choices=models.Class.objects.values_list('id', 'title'), attrs={'class': 'form-control'}
        )
    )


class TeacherForm(BootstrapForm, ModelForm):
    # name = fields.CharField(
    #     label='姓名',
    #     min_length=2,
    #     max_length=6,
    #     widget=widgets.TextInput(attrs={'class': 'form-control'})
    # )
    clss = fields.MultipleChoiceField(
        label='任教班级(多选)',
        choices=models.Class.objects.values_list('id', 'title'),
        widget=widgets.SelectMultiple
    )

    class Meta:
        model = models.Teacher
        fields = ['name', 'clss']


class LoginForm(BootstrapForm, Form):
    user = fields.CharField(
        label='姓名或账号',
        max_length=32,
        widget=widgets.TextInput(attrs={'class': 'form-control'})
    )
    password = forms.CharField(
        label='密码',
        min_length=4,
        max_length=64,
        error_messages={
            'min_length': "密码长度不能小于4个字符",
            'max_length': "密码长度不能大于64个字符"
        },
        widget=forms.PasswordInput()
    )

    # def clean_password(self):
    #     pwd = self.cleaned_data['password']
    #     # 加密 & 返回
    #     return encrypt.md5(pwd)
    # 不设置管理员注册功能，初始账户是从数据库中直接添加
    # 此处加密会导致登录的时候即使输入正确密码也会被加密导致密码错误
